// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using NUnit.Framework;
using ValidationAspects.Exceptions;

namespace TestValidationAspects.Exceptions
{
	[TestFixture]
	public class TestValidateObjectException
	{
		[SetUp]
		public void Test_SetUp()
		{
			ValidateObjectException.ResetMessageFormat();
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void Test_Constructor_object_throws_for_null_instance_param()
		{
			new ValidateObjectException(null);
		}

		[Test]
		public void Test_Constructor()
		{
			object instance = new object();
			ValidateObjectException exception = new ValidateObjectException(instance);
			Assert.AreSame(instance, exception.Object);
		}

		[Test]
		public void Test_default_MessageFormat()
		{
			ValidateObjectException exception = new ValidateObjectException(new Dummy());
			Assert.AreEqual(string.Format("Object [type:{0}] failed validation.", typeof(Dummy)), ValidateObjectException.MessageFormat(exception));
		}

		[Test]
		public void Test_get_Message_executes_MessageFormat()
		{
			const string message = "custom message";
			ValidateObjectException.MessageFormat = e => message;
			Assert.AreEqual(message, new ValidateObjectException(new object()).Message);
		}

		[Test]
		public void Test_ResetMessageFormat()
		{
			const string message = "custom message";
			ValidateObjectException.MessageFormat = e => message;

			ValidateObjectException exception = new ValidateObjectException(new Dummy());
			Assert.AreEqual(message, exception.Message);

			ValidateObjectException.ResetMessageFormat();
			Assert.AreEqual(string.Format("Object [type:{0}] failed validation.", typeof(Dummy)), ValidateObjectException.MessageFormat(exception));
		}

		#region Mocks
		public class Dummy {}
		#endregion
	}
}